﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.Serialization;
#if NET5_0_OR_GREATER
using System.Text.Json;
using System.Text.Json.Serialization;
#endif //NET5_0_OR_GREATER

namespace Dot5Demo.Models
{
    public class PictureInfo
    {
        public static PictureInfo RandomInfo
        {
            get
            {
                var r = new Random(Environment.TickCount % 100);
                var info = new PictureInfo()
                {
                    File = "RandomInfo",
                    Size = Environment.WorkingSet,
                    Index = Environment.TickCount,
                    Infos = new List<Info>()
                    {
                        new Info("001", r.Next(100, 200)),
                        new Info("002", r.Next(200, 300)),
                        new Info("003", r.Next(300, 400)),
                    },
                    InfoDic = new Dictionary<string, Info>()
                    {
                        { "111", new Info("101", r.Next(100, 200)) },
                        { "222", new Info("201", r.Next(200, 300)) },
                        { "333", new Info("301", r.Next(300, 400)) },
                    },
                    Colors = new List<Color>()
                    {
                        Color.FromArgb(r.Next(1,1024)),
                        Color.FromArgb(r.Next(1024,2048)),
                        Color.FromArgb(r.Next(2048,4096)),
                    },
                    ColorDic = new Dictionary<int, Color>()
                    {
                        {1001, Color.FromArgb(r.Next(1,1024))},
                        {2001, Color.FromArgb(r.Next(1,1024))},
                        {3001, Color.FromArgb(r.Next(1,1024))},
                    },
                    Seqs = new List<int>()
                    {
                        r.Next(10),r.Next(100),r.Next(1000),
                    },
                    Seqs2 = new int[]
                    {
                        r.Next(10),r.Next(100),r.Next(1000),
                    },
                };
                return info;
            }
        }

        public string File { get; set; }

#if NET5_0_OR_GREATER
        [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
#endif //NET5_0_OR_GREATER
        public long Size { get; set; }
#if NET5_0_OR_GREATER
        [JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
#endif //NET5_0_OR_GREATER
        public int Index { get; set; }

        public IList<int> Seqs { get; set; }
        public int[] Seqs2 { get; set; }

#if NET5_0_OR_GREATER
        [JsonPropertyName("Info List")]
#endif //NET5_0_OR_GREATER
        public List<Info> Infos { get; set; }

#if NET5_0_OR_GREATER
        [JsonPropertyName("Color List")]
#endif //NET5_0_OR_GREATER
        public List<Color> Colors { get; set; }

        //[JsonPropertyName("Info Dictionary")]
        public Dictionary<string, Info> InfoDic { get; set; }

        public Dictionary<int, Color> ColorDic { get; set; }

        /// <summary>
        /// 捕获额外数据(只能有一个)
        /// </summary>
#if NET5_0_OR_GREATER
        [JsonExtensionData]
#endif //NET5_0_OR_GREATER
        public Dictionary<string, object> ExtensionData { get; set; }

        ///// <summary>
        ///// 捕获额外数据(只能有一个)
        ///// </summary>
        //[JsonExtensionData]
        //public Dictionary<string, JsonElement> ExtensionData2 { get; set; }

        public bool Check()
        {
            var r = true;
            if (Infos != null)
            {
                foreach (var item in Infos)
                {
                    if (item != null && !item.Check())
                        return false;
                }
            }
            if (InfoDic != null)
            {
                foreach (var item in InfoDic)
                {
                    if (item.Value != null && !item.Value.Check())
                        return false;
                }
            }
            return r;
        }
    }

    public class Info
    {
        public Info()
        {
        }

        /// <summary>
        /// 指定反序列化的构造函数
        /// </summary>
        /// <param name="name"></param>
        /// <param name="id"></param>
        //[JsonConstructor]
        public Info(string name, int id)
        {
            Name = name;
            ID = id;
            AutoID = GetAutoID();
        }

#if NET5_0_OR_GREATER
        [JsonInclude]
#endif //NET5_0_OR_GREATER
        public string Name { get;private set; }
#if NET5_0_OR_GREATER
        [JsonInclude]
#endif //NET5_0_OR_GREATER
        public int ID { get; private set; }

#if NET5_0_OR_GREATER
        [JsonIgnore]
#endif //NET5_0_OR_GREATER
        public int AutoID { get; private set; }
        public int GetAutoID()
        {
            if (Name == null)
                return ID * 1000 + 123;
            else
                return Name.GetHashCode() + ID;
        }

        public bool Check()
        {
            return AutoID == GetAutoID();
        }
    }

    public abstract class Picture
    {
        public string File { get; set; }
        public string Name { get; set; }

        /// <summary>
        /// 忽略指定序列化属性
        /// </summary>
#if NET5_0_OR_GREATER
        [JsonIgnore]
#endif //NET5_0_OR_GREATER
        public Image Img { get; set; }
    }

    public class BmpPic : Picture
    {
        public ComplexClass Complex { get; set; }
    }

    [DataContract]
    public class BitmapPic : Picture
    {
        [DataMember]
        public string Value { get; set; }

        [IgnoreDataMember]
        public Bitmap ImgEx { get; set; }
    }

    public class ComplexClass
    {
        public ComplexClass(int a, int b, int c)
        {
            A = a;
            B = b;
            C = c;
        }

        public int A { get; set; }
        public int B { get; set; }
        public int C { get; set; }
    }
}